import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Canvas;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Rectangle;


public class MainView extends JFrame implements MouseListener{

	private JFrame frame;
	JPanel panel;
	
	int xSize = 5;
	int ySize = 4;
	JLabel[][] images;
	int clickCount = 0;
	int match1X = -1;
	int match1Y = -1;
	int match2X = -1;
	int match2Y = -1;
	int matchesLeft = (xSize*ySize)/2;
	ArrayList<Integer> imagesNumbers;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainView window = new MainView();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
	

	/**
	 * Create the application.
	 * @throws IOException 
	 */
	public MainView() throws IOException {
		initialize();
	}

	
	
	
	/**
	 * Initialize the contents of the frame.
	 * @throws IOException 
	 */
	private void initialize() throws IOException {
		
		frame = new JFrame();
		frame.setBounds(0, 0, (xSize*101)+36, (ySize*101)+61);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		panel = new JPanel(){
			
			protected void paintComponent(Graphics g)
			{
				super.paintComponent(g);
	  			Graphics2D g2d = (Graphics2D) g;
	  			g2d.drawImage(scale(new ImageIcon("background.jpg").getImage(), panel.getWidth(), panel.getHeight()).getImage(), 0, 0, this);
			}
		};
		panel.setBounds(10, 10, 1+xSize*101, 1+ySize*101);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		panel.add(new JLabel(scale(new ImageIcon("background.jpg").getImage(), panel.getWidth(), panel.getHeight())));
		panel.addMouseListener(this);
		

		images = new JLabel[xSize][ySize];
		
		for(int i = 0; i < xSize; i++)
			for(int j = 0 ; j < ySize; j++){
				
				images[i][j] = new JLabel(scale(new ImageIcon("default.jpg").getImage(),100,100));
				images[i][j].addMouseListener(this);
				images[i][j].setBounds(1+i*101, 1+j*101, 100, 100);
				images[i][j].setName(""+((j*xSize)+i));
				panel.add(images[i][j]);
				
			}
		
		assignImages();
		
		
	}
	

	
	/**
	 * Scales an image to the specified width and height
	 * 
	 * @param src the image to be scaled
	 * @param w the width of the new scaled image
	 * @param h the height of the new scaled image
	 * @return an ImageIcon of the new scaled image
	 */
	private ImageIcon scale(Image src, int w, int h) {
	
		int type = BufferedImage.TYPE_INT_RGB;
		BufferedImage dst = new BufferedImage(w, h, type);
		Graphics2D g2 = dst.createGraphics();
		g2.drawImage(src, 0, 0, w, h, this);
		g2.dispose();
		return new ImageIcon(dst);
		
	}
	
	
	
	private void assignImages(){
		
		imagesNumbers = new ArrayList<Integer>();
		
		for(int i = 0; i < matchesLeft; i++){
			imagesNumbers.add(i);
			imagesNumbers.add(i);
		}
		
		Collections.shuffle(imagesNumbers);
		
	}

	
	
	private void resetAll(){
		
		for(int i = 0; i < xSize; i++)
			for(int j = 0 ; j < ySize; j++){
				
				images[i][j].setIcon(scale(new ImageIcon("default.jpg").getImage(),100,100));
				
			}
		
	}
	
	
	
	// Mouse Listener:-
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
		if(e.getSource() == panel){
			
			if(matchesLeft == 0)
				System.exit(0);
			return;
			
		}
		
		
		clickCount++;
		if(clickCount == 1){
			match1X = (e.getComponent().getX()-1)/101;
			match1Y = (e.getComponent().getY()-1)/101;
		}else{
			
			if(clickCount == 2){
				match2X = (e.getComponent().getX()-1)/101;
				match2Y = (e.getComponent().getY()-1)/101;
				if(match1X == match2X && match1Y == match2Y)
					clickCount--;
			}else{
				
				if(clickCount == 3){
					
					if( imagesNumbers.get((match1Y*xSize)+match1X) != imagesNumbers.get((match2Y*xSize)+match2X) )
						resetAll();
					else{
						
						images[match1X][match1Y].setVisible(false);
						images[match2X][match2Y].setVisible(false);
						matchesLeft--;
						
						if(matchesLeft == 0)
							System.exit(0);
						
					}
						
					clickCount = 1;
					match1X = (e.getComponent().getX()-1)/101;
					match1Y = (e.getComponent().getY()-1)/101;
				}
			}
		}
		
		int blockID = Integer.parseInt(e.getComponent().getName());
		((JLabel) e.getComponent()).setIcon(scale(new ImageIcon(imagesNumbers.get(blockID)+".png").getImage(),100,100));
		
		if(matchesLeft == 1 && clickCount == 2){
			
			images[match1X][match1Y].setVisible(false);
			images[match2X][match2Y].setVisible(false);
			matchesLeft--;
			
		}
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	
}
